package cn.bluethink.view;

import cn.bluethink.utile.AuthenticationUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;


public class LoginView extends JFrame {

    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();

    JLabel l1 = new JLabel("id");
    JTextField f1 = new JTextField();

    JLabel l2 = new JLabel("password");
    JPasswordField  f2 = new JPasswordField ();

    JButton b1 = new JButton("提交");

    public Map<String, String> buildRegisteredView() {
        final Map<String, String> result = new HashMap<>();
        this.setLayout(new BorderLayout());
        p1.setLayout(new GridLayout(2, 1));
        p1.add(l1);
        p1.add(f1);

        p1.add(l2);
        p1.add(f2);
        this.add(p1, BorderLayout.NORTH);
        p2.add(b1, BorderLayout.CENTER);
        this.add(p2, BorderLayout.CENTER);

        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                result.put("userId", f1.getText());

                if (AuthenticationUtil.login(Integer.valueOf(f1.getText()), f2.getText())) {
                    close();
                } else {
                    JOptionPane.showMessageDialog(null, "账号或密码错误", "出错啦", JOptionPane.ERROR_MESSAGE);
                }

            }
        });
        this.setSize(250, 200);
        this.setVisible(true);
        return result;
    }

    private void close() {
        this.dispose();
    }
}
